package com.student;

import com.student.common.Subject;
import com.student.intf.IStudentVisitor;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class StudentManager {

    private Map<String, Student> students = new HashMap<>();



    private Map<String, Student> getStudents() {
        return this.students;
    }

//    public void setStudents(Map<String, Student> students) {
//        this.students = students;
//    }

    public int getTotalNumber() {
        return this.getStudents().size();
    }

    /**打印所有学生信息**/


    public void printAll() {
        Map<String, Student> ss = this.getStudents();

        for (Iterator<String> it = ss.keySet().iterator(); it.hasNext(); ) {
            String key = it.next();
            Student student = ss.get(key);
            int chineseScore = student.getChineseScore();
            int mathScore = student.getMathScore();
            int englishScore = student.getEnglishScore();
            int totalScore = chineseScore + mathScore + englishScore;
            int avgScore = totalScore / 3;
            System.out.println(String.format("学号：%s, 姓名：%s, 语文：%s, 数学：%s, 外语：%s, 总分：%s, 平均分：%s",
                    student.getId(), student.getName(),
                    chineseScore,mathScore,englishScore,totalScore,avgScore));
        }


    }



    public void visit(IStudentVisitor visitor) {
        this.getStudents().forEach((key, val) -> {
            visitor.visit(val);
        });
    }

    public void addStudent(Student student) {
        this.students.put(student.getId(), student);
    }

    public void deleteById(String id) {
        this.getStudents().remove(id);
    }

    public Student findById(String id) {
       return this.students.get(id);
    }

    public void saveAs(String filePath) throws IOException {
        Map<String, Student> ss = this.getStudents();
        FileWriter writer = new FileWriter(filePath);
        for (Iterator<String> it = ss.keySet().iterator(); it.hasNext(); ) {
            String key = it.next();
            Student student = ss.get(key);
            int chineseScore = student.getChineseScore();
            int mathScore = student.getMathScore();
            int englishScore = student.getEnglishScore();
            int total = chineseScore + mathScore + englishScore;
            int avgScore = (chineseScore + mathScore + englishScore) / 3;

            writer.write(String.format("%s,%s,%s,%s,%s,%s,%s\n",
                    student.getId(),
                    student.getName(),
                    chineseScore,
                    mathScore,
                    englishScore,
                    total,
                    avgScore
                    ));
        }
        writer.close();
    }

    /**根据ID查询**/

    public Student getStudentById(String id) {
        Map<String, Student> ss = this.getStudents();
        Student student = ss.get(id);
        if (student != null) {
            int chineseScore = student.getChineseScore();
            int mathScore = student.getMathScore();
            int englishScore = student.getEnglishScore();
            int totalScore = chineseScore + mathScore + englishScore;
            int avgScore = totalScore / 3;
            System.out.println(String.format("学号为：%s,的学生信息是：学号：%s, 姓名：%s, 语文：%s, 数学：%s, 外语：%s, 总分：%s, 平均分：%s",
                    id,student.getId(), student.getName(),
                    chineseScore, mathScore, englishScore, totalScore, avgScore));
        }
        return student;
    }

    /**根据成绩和科目**/

    public List<Student> findByScore(String classType,int min,int max){
        Map<String, Student> ss = this.getStudents();
        Collection<Student> studentCollection = ss.values();
        List<Student> studentList = new ArrayList<>();
        if(Subject.MATH.equals(classType)){
            studentList = studentCollection.stream().filter(student -> student.getMathScore()>=min && student.getMathScore()<=max).collect(Collectors.toList());
        }
        if(Subject.CHINESE.equals(classType)){
            studentList = studentCollection.stream().filter(student -> student.getChineseScore()>=min && student.getChineseScore()<=max).collect(Collectors.toList());
        }
        if(Subject.ENGLISH.equals(classType)){
            studentList = studentCollection.stream().filter(student -> student.getEnglishScore()>=min && student.getEnglishScore()<=max).collect(Collectors.toList());
        }
        System.out.format("课程：%s,最小分数：%s,最大分数：%s之间的学生信息为",classType,min,max);
        studentList.stream().forEach(student -> {
                int chineseScore = student.getChineseScore();
                int mathScore = student.getMathScore();
                int englishScore = student.getEnglishScore();
                int totalScore = chineseScore + mathScore + englishScore;
                int avgScore = totalScore / 3;
                System.out.println(String.format("学号：%s, 姓名：%s, 语文：%s, 数学：%s, 外语：%s, 总分：%s, 平均分：%s",
                        student.getId(), student.getName(),
                        chineseScore, mathScore, englishScore, totalScore, avgScore));

        });
        return studentList;
    }

    /**根据成绩平均分**/

    public List<Student> findByScore(int min,int max){
        Map<String, Student> ss = this.getStudents();
        Collection<Student> studentCollection = ss.values();
        List<Student> studentList = new ArrayList<>();
        studentList = studentCollection.stream().filter(s -> (s.getMathScore() + s.getEnglishScore() + s.getChineseScore()) / 3 >= min &&
                (s.getMathScore() + s.getEnglishScore() + s.getChineseScore()) / 3 <= max).collect(Collectors.toList());

        System.out.format("平均分在最小分数：%s,最大分数：%s之间的学生信息为",min,max);
        studentList.stream().forEach(student -> {
            int chineseScore = student.getChineseScore();
            int mathScore = student.getMathScore();
            int englishScore = student.getEnglishScore();
            int totalScore = chineseScore + mathScore + englishScore;
            int avgScore = totalScore / 3;
            System.out.println(String.format("学号：%s, 姓名：%s, 语文：%s, 数学：%s, 外语：%s, 总分：%s, 平均分：%s",
                    student.getId(), student.getName(),
                    chineseScore, mathScore, englishScore, totalScore, avgScore));

        });
        return studentList;
    }

    /**根据总分排序**/



    public List<Student> sortByTotal(){
        Map<String, Student> ss = this.getStudents();
        Collection<Student> studentCollection = ss.values();
        List<Student> studentList = new ArrayList<>(studentCollection);

        studentList.sort(Comparator.comparing((Student s) -> new Integer(s.getMathScore()+s.getEnglishScore())+s.getChineseScore()).reversed());
        System.out.format("根据总分由高到低排序后的学生信息为：");
        studentList.stream().forEach(student -> {
            int chineseScore = student.getChineseScore();
            int mathScore = student.getMathScore();
            int englishScore = student.getEnglishScore();
            int totalScore = chineseScore + mathScore + englishScore;
            int avgScore = totalScore / 3;
            System.out.println(String.format("学号：%s, 姓名：%s, 语文：%s, 数学：%s, 外语：%s, 总分：%s, 平均分：%s",
                    student.getId(), student.getName(),
                    chineseScore, mathScore, englishScore, totalScore, avgScore));

        });
        return studentList;
    }
}





